﻿using System;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

using SBPweb.RIA.Shared.Contracts.Base;
using SBPweb.RIA.Shared.Contracts.Models;

using SBPweb.RIA.Client.Contracts.Base;
using SBPweb.RIA.Client.Contracts.Models;

using SBPweb.RIA.Client.Controls.Controllers;

namespace SBPweb.RIA.Client.Controls.Models
{

    public class NavigationModel : ModelBase, INavigationModel
    {
        
        #region Private fields

        //private AuthProviderClient authClient;

        #endregion

        #region Constructor logic

        internal NavigationModel()
            : base()
        {
            //authClient = ServiceConnector.CreateServiceClientInstance<AuthProviderClient, IAuthProvider>();
            //authClient.StartAuditCompleted += new EventHandler<AsyncCompletedEventArgs>(authClient_StartAuditCompleted);
        }

        #endregion

        #region Dependency properties

        public static readonly DependencyProperty UriProperty = DependencyProperty.Register("Uri", typeof(Uri), typeof(NavigationModel), null);

        #endregion

        #region Property accessors

        public Uri Uri
        {
            get
            {
                return GetTypedValue<Uri>(UriProperty);
            }
            private set
            {
                SetTypedValue<Uri>(UriProperty, value, "Uri");
            }
        }

        public bool CanGoBack
        {
            get
            {
                return NavigationService != null && NavigationService.CanGoBack;
            }
        }

        public bool CanGoForward
        {
            get
            {
                return NavigationService != null && NavigationService.CanGoForward;
            }
        }

        internal NavigationService NavigationService
        {
            get;
            private set;
        }

        #endregion

        #region Events

        public event EventHandler<NavigatorCancelEventArgs> Navigating;

        public event EventHandler<NavigatorResultEventArgs> Navigation;
        
        public event EventHandler<NavigatorEventArgs> Navigated;

        #endregion

        #region Event handlers

        internal void OnNavigating(Uri url)
        {
            OnNavigating(url, false);
        }

        internal void OnNavigating(Uri url, bool cancel)
        {
            OnNavigating(url, NavigationMode.New, cancel);
        }

        internal void OnNavigating(Uri url, NavigationMode mode)
        {
            OnNavigating(url, mode, false);
        }

        internal void OnNavigating(Uri url, NavigationMode mode, bool cancel)
        {
            if (Navigating != null)
            {
                NavigatorCancelEventArgs ncea = new NavigatorCancelEventArgs(url, mode, cancel);
                Navigating(this, ncea);
            }
        }

        internal bool OnNavigation(Uri url)
        {
            bool ret = false;
            if (Navigation != null)
            {
                NavigatorResultEventArgs ncea = new NavigatorResultEventArgs(url);
                Navigation(this, ncea);
                return ncea.Result;
            }
            return ret;
        }

        internal void OnNavigated(Uri url)
        {
            OnNavigated(url, null);
        }

        internal void OnNavigated(Uri url, object content)
        {
            this.Uri = url;
            Page page = content as Page;
            if (page != null)
            {
                this.NavigationService = page.NavigationService;
            }
            if (Navigated != null)
            {
                Navigated(this, new NavigatorEventArgs(url, content));
            }
        }

        #endregion

    }

}